import axios from 'axios'
import Qs from 'qs'
import {
    Indicator,
    Toast
} from 'mint-ui';

// // 模拟imei
// if (!localStorage.getItem('IMEI')) localStorage.setItem('IMEI', UT.uuid())
// const IMEI = localStorage.getItem('IMEI')

const PROJECTNAME = location.pathname.split(/\//)[1];
let BASEURL = null
if (PROJECTNAME) {
    BASEURL = location.origin + '/' + PROJECTNAME + '/';
} else {
    BASEURL = location.origin + '/';
}
/*
 浏览器地址为 http://ip:port/project/#/hash
 BASEURL = http://ip:port/project/

 浏览器地址为 http://ip:port/#/hash
 BASEURL = http://ip:port/
 */

Object.defineProperties(window, {
    __BASESERVER__: {
        value: BASEURL
    },
    __IMGSERVER__: {
        value: BASEURL
    },
    __FILESERVER__: {
        value: BASEURL
    }
})

const baseConfig = {
    baseURL: __BASESERVER__,
    transformResponse: [function(data) {
        Indicator.close();
        return data;
    }],
    headers: {},
    params: {
        // 'imei': IMEI
    },
    paramsSerializer: function(params) {
        return Qs.stringify(params)
    },
    validateStatus: (status) => {
        // "-1";	//系统繁忙，请稍后再试
        // "0";	//请求成功
        // "-1009"; //当前用户未认证房产信息
        // "-1002"; //用户不存在
        // "-1000"; //获取数据异常
        switch (status) { //后期可以在这里处理一下请求失败的状态码
            case 500:
                // Toast({
                //     message:'请求失败'
                // })
                return false;
            case 504:
                Toast({
                    message: '504 – 网关超时'
                })
                return false;
        }
        return status >= 200 && status < 300; // 默认的
    },
    timeout: 3 * 60 * 1000,
    responseType: 'json'
}

const IndicatorFilter = (url) => {
    const whiteList = [
        'restful/message/inputTips'
    ]

    for (let v of whiteList) {
        if (v === url) {
            return true
        }
    }
}
const _http = axios.create(baseConfig);
_http.interceptors.request.use(function(config) {
    if (!IndicatorFilter(config.url)) {
        Indicator.open();
    }
    return config;
}, function(error) {
    // 对请求错误做些什么
    console.log(error);
    return Promise.reject(error);
});

_http.interceptors.response.use(function(response) {
    // Do something with response data
    // console.log(status)
    return response.data;

}, function(error) {
    console.log(error)
    Indicator.close();
    if (!navigator.onLine) {
        Toast({
            message: '无法连接网络',
            position: 'center'
        });
    } else {
        Toast({
            message: '连接服务器失败',
            position: 'bottom'
        });
    }
    return Promise.reject(error);
});

const HttpService = {
    post: (url, data, config) => {
        const _selfConfig = {
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
            },
        }
        config = Object.assign({}, _selfConfig, config)
        return _http.post(url, data, config)
    },
    postObj: (url, data, config) => {
        const _selfConfig = {
            headers: {
                'Content-Type': 'application/json',
            }
        }
        config = Object.assign({}, _selfConfig, config)
        return _http.post(url, JSON.stringify(data), config)
    },
    postParams: (url, data, config) => {
        url += `?${Qs.stringify(data)}`
        const _selfConfig = {}
        config = Object.assign({}, _selfConfig, config)
        return _http.post(url, null, config)
    },
    get: (url, data, config) => {
        const _selfConfig = {
            params: data
        }
        config = Object.assign({}, _selfConfig, config)
        return _http.get(url, config)
    }
}

export default HttpService